package isvp.ui.beans;

import isvp.constants.UIConstants;
import isvp.interfaces.ISVP;
import isvp.tree.customizations.ISVPTreeNode;
import java.awt.Color;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author jjophin
 */
public class ISVPTree extends JTree {
    
    private ISVP isvpObject;

    /**
     * Creates a tree object and sets the basic look and feel of the tree
     */
    public ISVPTree() {
        setModel(null);
        getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        //Adding UI effects
        DefaultTreeCellRenderer treeCellRenderer = (DefaultTreeCellRenderer)getCellRenderer();
        treeCellRenderer.setBackgroundNonSelectionColor(new Color(236, 233, 216));

        //To get the dotted lines that connects various nodes in the tree
        putClientProperty("JTree.lineStyle", "Angled");        
    }

    public ISVP getIsvpObject() {
        return isvpObject;
    }

    public void setIsvpObject(ISVP isvpObject) {
        this.isvpObject = isvpObject;
    }
    
    public void renderISVPTree(TreeMap<String, ISVPJPanel> panelMap) {
        //Creating and adding the ISVP Node
        ISVPJPanel rootObject = panelMap.get(UIConstants.ISVP_NODE_NAME);
        ISVPTreeNode rootNode = new ISVPTreeNode(rootObject, UIConstants.ISVP_NODE_NAME, true, isvpObject);
        panelMap.remove(UIConstants.ISVP_NODE_NAME);
        
        Object componentData = isvpObject.getApplication();
        ISVPJPanel componentPanel = panelMap.get(UIConstants.APPLICATION_NODE_NAME);
        ISVPTreeNode componentNode = getComponentNode(componentPanel, UIConstants.APPLICATION_NODE_NAME, componentData);
        rootNode.add(componentNode);
        
        TreeMap componentMap = isvpObject.getPrivileges();
        componentPanel = panelMap.get(UIConstants.PRIVILEGES_NODE_NAME);
        ISVPTreeNode privilegeNode = getComponentNode(componentPanel, componentMap, UIConstants.PRIVILEGES_NODE_NAME);
        rootNode.add(privilegeNode);
        
        //Creating and setting the model
        DefaultTreeModel newTreeModel = new DefaultTreeModel(rootNode);
        setModel(newTreeModel);
    }
    
    private ISVPTreeNode getComponentNode(ISVPJPanel componentPanel, TreeMap componentMap, String rootNodeName) {
        //Creating the componentData root node
        ISVPTreeNode rootNode = new ISVPTreeNode(componentPanel, rootNodeName, true);
        
        //Navigating the componentData data map and adding the componentData entries
        String nodeName = "";
        ISVPTreeNode childNode = null;
        Object dataObject = null;
        Iterator it = componentMap.entrySet().iterator();
        Entry mapEntry = null;
        while(it.hasNext()) {
            mapEntry = (Entry)it.next();
            dataObject = mapEntry.getValue();
            nodeName = (String)mapEntry.getKey();
            childNode = new ISVPTreeNode(componentPanel, nodeName, false, dataObject);
            childNode.setParentNodeName(rootNodeName);
            rootNode.add(childNode);
        }
        return rootNode;
    }
    
    private ISVPTreeNode getComponentNode(ISVPJPanel componentPanel, String componentName, Object componentData) {
        ISVPTreeNode componentNode = new ISVPTreeNode(componentPanel, componentName, true, componentData);
        componentNode.setParentNodeName(componentName);
        return componentNode;
    }
}
